package db;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 	* A data access object (DAO) providing persistence and search support for Purmat entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see db.Purmat
  * @author MyEclipse Persistence Tools 
 */

public class PurmatDAO extends HibernateDaoSupport  {
    private static final Log log = LogFactory.getLog(PurmatDAO.class);
	//property constants
	public static final String PURREQ_ID = "purreqId";
	public static final String MATERIAL_ID = "materialId";
	public static final String NUMBER = "number";
	public static final String PRICE = "price";
	public static final String DECREASE = "decrease";
	public static final String BUY_NUM = "buyNum";
	public static final String STATE = "state";
	public static final String TMPPUR_ID = "tmppurId";
	public static final String WARE_NUM = "wareNum";



	protected void initDao() {
		//do nothing
	}
    
    public void save(Purmat transientInstance) {
        log.debug("saving Purmat instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Purmat persistentInstance) {
        log.debug("deleting Purmat instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Purmat findById( java.lang.Integer id) {
        log.debug("getting Purmat instance with id: " + id);
        try {
            Purmat instance = (Purmat) getHibernateTemplate()
                    .get("db.Purmat", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Purmat instance) {
        log.debug("finding Purmat instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Purmat instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Purmat as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByPurreqId(Object purreqId
	) {
		return findByProperty(PURREQ_ID, purreqId
		);
	}
	
	public List findByMaterialId(Object materialId
	) {
		return findByProperty(MATERIAL_ID, materialId
		);
	}
	
	public List findByNumber(Object number
	) {
		return findByProperty(NUMBER, number
		);
	}
	
	public List findByPrice(Object price
	) {
		return findByProperty(PRICE, price
		);
	}
	
	public List findByDecrease(Object decrease
	) {
		return findByProperty(DECREASE, decrease
		);
	}
	
	public List findByBuyNum(Object buyNum
	) {
		return findByProperty(BUY_NUM, buyNum
		);
	}
	
	public List findByState(Object state
	) {
		return findByProperty(STATE, state
		);
	}
	
	public List findByTmppurId(Object tmppurId
	) {
		return findByProperty(TMPPUR_ID, tmppurId
		);
	}
	
	public List findByWareNum(Object wareNum
	) {
		return findByProperty(WARE_NUM, wareNum
		);
	}
	

	public List findAll() {
		log.debug("finding all Purmat instances");
		try {
			String queryString = "from Purmat";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Purmat merge(Purmat detachedInstance) {
        log.debug("merging Purmat instance");
        try {
            Purmat result = (Purmat) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Purmat instance) {
        log.debug("attaching dirty Purmat instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Purmat instance) {
        log.debug("attaching clean Purmat instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static PurmatDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (PurmatDAO) ctx.getBean("PurmatDAO");
	}
	
	public List findByTwoPropertys(String[] propertyNames, Object[] values){
		log.debug("finding Purmat instance with property: " + propertyNames
				+ ", value: " + values);
		try {
			String queryString = "from Purmat as model where model."
					+ propertyNames[0] + "= ?" + " and model." + propertyNames[1] + "= ?";
			return getHibernateTemplate().find(queryString, values);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByPurreqIdAndState(Object[] values){
		return findByTwoPropertys(new String[]{PURREQ_ID, STATE}, values);
	}
	
}